package svc

import (
	"gitee.com/Cauchy_AQ/vxopen/api/internal/config"
	"gitee.com/Cauchy_AQ/vxopen/api/internal/middleware"
	"gitee.com/Cauchy_AQ/vxopen/rpc/transform/transformer"
	"gitee.com/Cauchy_AQ/vxopen/rpc/wechat/wechatclient"
	chat "gitee.com/Cauchy_AQ/vxopen/ws/chat"
	dashboard "gitee.com/Cauchy_AQ/vxopen/ws/dashboard"
	"github.com/zeromicro/go-zero/rest"
	"github.com/zeromicro/go-zero/zrpc"
)

type ServiceContext struct {
	Config                 config.Config
	Transformer            transformer.Transformer
	Wechat                 wechatclient.Wechat
	WSChatHub              *chat.Hub
	WSDashboardHub         *dashboard.Hub
	MetadataMiddleware     rest.Middleware
	CaptchaLimitMiddleware rest.Middleware
	AuthMiddleware         rest.Middleware
}

func NewServiceContext(c config.Config) *ServiceContext {
	transformers := transformer.NewTransformer(zrpc.MustNewClient(c.Transform))
	wechat := wechatclient.NewWechat(zrpc.MustNewClient(c.Wechat))
	wsChatHub := chat.NewHub(transformers)
	wsDashboardHub := dashboard.NewHub(transformers)
	go func() { wsChatHub.Run() }()
	go func() { wsDashboardHub.Run() }()
	return &ServiceContext{
		Config:                 c,
		Transformer:            transformers,
		Wechat:                 wechat,
		WSChatHub:              wsChatHub,
		WSDashboardHub:         wsDashboardHub,
		MetadataMiddleware:     middleware.NewMetadataMiddleware().Handle,
		CaptchaLimitMiddleware: middleware.NewCaptchaLimitMiddleware(transformers).Handle,
		AuthMiddleware:         middleware.NewAuthMiddleware().Handle,
	}
}
